<?php require_once("header.php"); ?>
	<div id="content">
		<div id="content-title">
			All List
		</div>
           <div id="isi-content">
				<?php
					if (!isset($_SESSION['login_username'])) {
						$disabled = "disabled=true";
					}
					else {
						$disabled = "disabled=true";
					}
					
					$con = mysql_connect("localhost","progin","progin");
				
					if (!$con) {
						die('Could not connect: ' . mysql_error());
					}

					mysql_select_db("progin_171_13509045", $con) or die("Cannot find database");
					
					$tbl_name="konten";
					$adjacents = 3;
					$tag = $_GET['tag'];

					if (strcmp($tag, 'nofilter') == 0) {
						$query = "SELECT COUNT(*) as num FROM $tbl_name";
					}
					else {
						$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE Tag='$tag'";
					}
					
					$total_content = mysql_fetch_array(mysql_query($query));
					$total_content = $total_content['num'];
	
					$targetpage = "list.php";
					$limit = 10;
					$page = $_GET['page'];
					
					if ($page) {
						$start = ($page - 1) * $limit;
					}
					else {
						$start = 0;
					}
	
					if (strcmp($tag, 'nofilter') == 0) {
						$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
						$result = mysql_query($sql);
					}
					else {
						$sql = "INSERT INTO tagfilter SELECT * FROM $tbl_name WHERE Tag='$tag'";
						$result = mysql_query($sql);
						$sql = "SELECT * FROM tagfilter LIMIT $start, $limit";
						$result = mysql_query($sql);
					}
					
					if ($page == 0) $page = 1;
					$prev = $page - 1;
					$next = $page + 1;
					$lastpage = ceil($total_content/$limit);
					$lpm1 = $lastpage - 1;
	
					$pagination = "";
					if ($lastpage > 1) {	
						$pagination .= "<div class=\"pagination\">";
						//previous button
						if ($page > 1) {
							$pagination.= "<a href=\"$targetpage?page=$prev&tag=$tag\">previous </a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">previous </span>";
						}
		
						//pages	
						if ($lastpage < 7 + ($adjacents * 2)) {	
							for ($counter = 1; $counter <= $lastpage; $counter++) {
								if ($counter == $page) {
									$pagination.= "<span class=\"current\">$counter </span>";
								}
								else {
									$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
								}
							}
						}
						else if ($lastpage > 5 + ($adjacents * 2))	{ //enough pages to hide some
							//close to beginning; only hide later pages
							if($page < 1 + ($adjacents * 2)) {
								for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
								
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//in middle; hide some front and some back
							else if ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
							
								for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter</span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
							
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//close to end; only hide early pages
							else {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
				
								for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
									}
								}
							}
						}
		
						//next button
						if ($page < $counter - 1) {
							$pagination.= "<a href=\"$targetpage?page=$next&tag=$tag\">next</a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">next</span>";
							$pagination.= "</div>\n";
						}
					}

					print "<table>";		
					$increment = 1;
					
					while ($row = mysql_fetch_array($result)) {
						$labelid = "likecount" + $increment;
						$contentID = $row['id_konten'];
						$like = $row['jumlahlike'];
						echo "<h3><a href=\"content.php?contentid=".$row['id_konten']."\" style=\"text-decoration:none;\">";
						echo $row['judul'];
						print '</a></h3>
								<div>
									<span>posted by</span>
									<span id="content-poster" class="commentator">';
						echo $row['username'];
						print '</div>
								<img border="0" src="'.$row['url'].'" alt="Pulpit rock" width="304" height="228" />
								<br/>
								<div id="likedislike">';						
						echo "<input type='button' id='likebutton' value='like' onclick=\"changeLike('$contentID', '$like', '$labelid', 'up');\" $disabled></input>
								<input type='button' id='dislikebutton' value='dislike' onclick=\"changeLike('$contentID', '$like', '$labelid', 'down');\" $disabled></input>
								<label id='$labelid'>";
						echo $row['jumlahlike'];
						print ' likes</label>
								</div>
								<div>Jumlah komentar ';
						echo $row['jumlahkomentar'];
						print '</div>';
						$increment = $increment + 1;
					}

					print "</table>";
					
					echo "<div>
								<a href=\"$targetpage?page=1&tag=nofilter\">Lihat Semua Tanpa Tag</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Bencana akibat korupsi\">Bencana akibat korupsi</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Kejahatan terhadap alam\">Kejahatan terhadap alam</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Pelanggaran lalu lintas\">Pelanggaran lalu lintas</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Korupsi\">Korupsi</a>
							</div>";
							
					$sql ="TRUNCATE TABLE tagfilter";
					$result = mysql_query($sql);
	
					mysql_close($con);
				?> 
			</div>
	</div>
	<?=$pagination?>
<?php require_once("footer.php"); ?>

